﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.RBACs;
using MangoMes.Commont.Domain.Suppliers;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;
using System;

namespace MangoMes.Write.API.Applications.SupplierSearchWrite
{
    public class SupplierCreateHandler : IRequestHandler<SupplierCreateCommand, ResultsDTO>
    {
        private readonly IBateRepository<Supplier> _supplierRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="supplierRepository"></param>
        public SupplierCreateHandler(IBateRepository<Supplier> supplierRepository)
        {
            _supplierRepository = supplierRepository;
        }
        /// <summary>
        /// 供应商新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultsDTO> Handle(SupplierCreateCommand request, CancellationToken cancellationToken)
        {
            DapperHelper dapperHelper = new DapperHelper();
            ResultsDTO res = new ResultsDTO { Code = 0 };
            Supplier per = new Supplier();
            per.SupplierId = request.SupplierId;
            per.SupplierNo = request.SupplierNo;
            per.SupplierName = request.SupplierName;
            per.SupplierShort = request.SupplierShort;
            per.SupplierEnglishName = request.SupplierEnglishName;
            per.SupplierIntro = request.SupplierIntro;
            per.SupplierAddres = request.SupplierAddres;
            per.SupplierClassId = request.SupplierClassId;
            per.SupplieScore = request.SupplieScore;
            per.SupplierOfficialAddres = request.SupplierOfficialAddres;
            per.SupplierEmailAddres = request.SupplierEmailAddres;
            per.SupplierPhone = request.SupplierPhone;
            per.SupplierOnePeople = request.SupplierOnePeople;
            per.SupplierOnePhone = request.SupplierOnePhone;
            per.SupplierOneEmail = request.SupplierOneEmail;
            per.SupplierTwoPeople = request.SupplierTwoPeople;
            per.SupplierTwoPhone = request.SupplierTwoPhone;
            per.SupplierTwoEmail = request.SupplierTwoEmail;
            per.SupplierCredit = request.SupplierCredit;
            per.SupplierIsNo = request.SupplierIsNo;
            per.SupplierLOGO = request.SupplierLOGO;
            per.SupplierRemark = request.SupplierRemark;
            per.SupplierStart = 0;
            string conn = dapperHelper.MangoMesWrite;
            string str = "insert into supplier values(@SupplierId,@SupplierNo,@SupplierName,@SupplierShort,@SupplierEnglishName,@SupplierIntro,@SupplierAddres,@SupplierClassId,@SupplieScore,@SupplierOfficialAddres,@SupplierEmailAddres,@SupplierPhone,@SupplierOnePeople,@SupplierOnePhone,@SupplierOneEmail,@SupplierTwoPeople,@SupplierTwoPhone,@SupplierTwoEmail,@SupplierCredit,@SupplierIsNo,@SupplierLOGO,@SupplierRemark,@SupplierStart)";
            res.Code = await _supplierRepository.Add(str, per, conn);
            return res;
        }
    }
}
